package com.itheima.controller;

import com.itheima.Service.UserService;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.pojo.UserQueryVO;
import com.itheima.pojo.UserResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserAdminController {

    private static final Logger log = LoggerFactory.getLogger(UserAdminController.class);
    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     */
    @PostMapping("/list")
    public Result listUsers(@RequestBody UserQueryVO queryVO) {
        List<UserResultVO> userList = userService.listUsers(queryVO);
        int total = userService.countUsers(queryVO);

        Map<String, Object> result = new HashMap<>();
        result.put("list", userList);
        result.put("total", total);

        return Result.success(result);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/detail/{id}")
    public Result getUserDetail(@PathVariable Integer id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public Result addUser(@RequestBody User user) {
        boolean result = userService.addUser(user);
        return result ? Result.success() : Result.error("添加用户失败");
    }

    /**
     * 更新用户
     */
    @PostMapping("/update/{id}")
    public Result updateUser(@RequestBody User user) {
log.info("用户信息：{}", user);
        boolean result = userService.updateUser(user);
        return result ? Result.success() : Result.error("更新用户失败");
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        boolean result = userService.deleteUser(id);
        return result ? Result.success() : Result.error("删除用户失败");
    }

    /**
     * 锁定用户
     */
    @PostMapping("/lock/{id}")
    public Result lockUser(@PathVariable Integer id) {
        boolean result = userService.lockUser(id);
        return result ? Result.success("用户锁定成功") : Result.error("用户锁定失败");
    }

    /**
     * 解锁用户
     */
    @PostMapping("/unlock/{id}")
    public Result unlockUser(@PathVariable Integer id) {
        boolean result = userService.unlockUser(id);
        return result ? Result.success("用户解锁成功") : Result.error("用户解锁失败");
    }
}